package org.test.client.ui.component.field;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

/**
 * 高亮 边框 输入框 带图标 带placeholder
 * 
 * 禁用 与 禁止编辑 效果暂时未实现
 * @author Guodeqi
 * @email 305531834@qq.com
 *
 */
public class HighLightTextField extends JTextField implements FocusListener{
	
	public static Color blueColor = new Color(Integer.parseInt("66afe9",16));
	public static Color greenColor = new Color(Integer.parseInt("468847",16));
	public static Color orangeColor = new Color(Integer.parseInt("c09853",16));
	public static Color redColor = new Color(Integer.parseInt("b94a48",16));
	
	//高亮时候 启动一个线程 使效果看起来更平滑，每次 给外面再画一个蓝边框
	public int hightLightLevel = 0;
	
	public Color highColor = blueColor;
	//是否需要 圆角
	public boolean needRound = true;
	private int borderWidth = 4;
	private int diameter = 16;
	public Color borderColor = new Color(Integer.parseInt("cccccc",16));
	private Color backGrondColor = new Color(Integer.parseInt("ffffff",16));
	
	private Color iconBackColor = Icons.getColor("eeeeee");
	public Color iconColor = Icons.getColor("555555");
	
	private int insectLeft = diameter;
	private int insectRight =  diameter;
	public int iconMarginLR = diameter/2 + 6 ;
	
	//留给 图标鼠标事件 使用
	private Rectangle rectStart = null;
	private Rectangle rectEnd = null;
	
	public TxtFieldSE start = null;
	public TxtFieldSE end = null;
	
	public boolean haveFocus = false;
	public String placeHolder = null;//"place holder";
	//是否需要高亮
	public boolean needHighLight = true;
	
	public HighLightTextField(){
		this.addFocusListener(this);
		this.setOpaque(false);
	}
	
	public HighLightTextField(TxtFieldSE s ,TxtFieldSE e){
		this.addFocusListener(this);
		this.start = s;
		this.end = e;
		this.setOpaque(false);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		// this.setBackground(this.getParent().getBackground());
		Graphics2D g2d = (Graphics2D)g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		int compw = this.getWidth();
		int comph = this.getHeight();
		
		g2d.setColor(backGrondColor);
		g2d.fillRoundRect(borderWidth, borderWidth, compw-borderWidth*2, comph-borderWidth*2, diameter, diameter);
		
		rectStart = drawBlock(start,g2d,compw,comph);
		rectEnd = drawBlock(end,g2d,compw,comph);
		
		if(this.getText().length()==0 && placeHolder!=null){
			g2d.setColor(Color.lightGray);
			g2d.drawString(placeHolder, getInsets().left, comph/2 + UIUtil.getTxty(g2d));
		}
		
		super.paintComponent(g);
	}
	
	@Override
	protected void paintBorder(Graphics g) {
		Graphics2D g2d = (Graphics2D)g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		int compw = this.getWidth();
		int comph = this.getHeight();
		
		g.setColor(borderColor);
		if(needRound){
			g.drawRoundRect(borderWidth, borderWidth, compw-borderWidth*2-1,comph-borderWidth*2-1, diameter, diameter);
		}else{
			g.drawRect(borderWidth, borderWidth, compw-borderWidth*2-1,comph-borderWidth*2-1);
		}
		
		if(haveFocus){
			g2d.setColor(highColor);
			int x = borderWidth;
			int y =borderWidth;
			int w= compw-borderWidth*2-1;
			int h = comph-borderWidth*2-1;
			if(needRound){
				g2d.drawRoundRect(x,y,w,h, diameter, diameter);
			}else{
				g.drawRect(x,y,w,h);
			}
			for(int i=0;i<hightLightLevel;i++){
				float a = 0.3f - (float)i*0.1f;
				AlphaComposite ac = UIUtil.getSrcOverAC(a);
				g2d.setComposite(ac);
				x = x-1;
				y= y-1;
				w= w + 2;
				h= h + 2;
				if(needRound){
					g2d.drawRoundRect(x,y,w,h, diameter, diameter);
				}else{
					g.drawRect(x,y,w,h);
				}
			}
		}
	}
	
	private Rectangle drawBlock(TxtFieldSE block, Graphics2D g2d, int compw, int comph) {
		if(block == null){
			return null;
		}
		g2d.setColor(Color.darkGray);
		g2d.setFont(this.getFont());
		int iconh = UIUtil.getTxtHeight(g2d);
		int iconw = iconh;
		BufferedImage bi = null;
		if(!block.fontAwareIcon){
			iconw = UIUtil.getTxtWidth(g2d, block.info);
		}else{
			bi = Icons.getImgByDesc(block.info, iconw, iconColor);
		}
		int x = 0;
		int y = 0;
		
		if(block.start){
			x = borderWidth + iconMarginLR;
			y = comph/2 - iconh/2;
			insectLeft = iconw + iconMarginLR*2 +borderWidth;
		}else{
			insectRight = iconw + iconMarginLR*2 + borderWidth;
			//加2 是因为暂时 找不到 哪出错了。如果右侧加图标 会比左侧的宽
			x = compw - insectRight + diameter/2 + 2;
			y = comph/2 - iconh/2;
		}

		//draw content
		if(block.fontAwareIcon){
			g2d.drawImage(bi, x, y, iconw,iconh	,null);
		}else{
			//drawString
			g2d.drawString(block.info, x, comph/2+UIUtil.getTxty(g2d)); 
		}
		g2d.setColor(borderColor);
		
		int tmpx = x;
		if(block.start){
			tmpx = x + iconw + iconMarginLR;
		}else{
			tmpx = x - iconMarginLR;
		}
		if(block.haveBorder){
			g2d.drawLine(tmpx, borderWidth+1, tmpx, comph-borderWidth*2+2);
		}
			
		
		return new Rectangle(x,y,iconw,iconh);
	}

	@Override
	public Insets getInsets() {
		int top = borderWidth;
		
		int left = insectLeft ;
		if(needRound ){
			left += diameter/2 ;
		}
		int bottom = borderWidth;
		int right = insectRight + diameter/2 ;
		return new Insets(top, left, bottom, right);
	}
	
	@Override
	public void focusGained(FocusEvent focusevent) {
		if(needHighLight){
			haveFocus = true;
			LightRun run = new LightRun(this);
			new Thread(run).start();
		}
	}

	@Override
	public void focusLost(FocusEvent focusevent) {
		if(needHighLight){
//			在线层里面设置 为false
//			haveFocus = false; 
			LightRun run = new LightRun(this, false);
			new Thread(run).start();
		}
	}
	
	/** **********************  test ********************** **/
	public static void main(String[] args) {
		
		JFrame frame = new JFrame();
		JPanel jPanel = new JPanel();
		
		HighLightTextField txtf = new HighLightTextField(TxtFieldSE.createStartIcon("fa-glass"),TxtFieldSE.createEndInfo("@qq.com"));
		txtf.setFont(new Font(Font.DIALOG, Font.PLAIN, 18));
		txtf.setPreferredSize(new Dimension(310,50));
		jPanel.add(txtf);
		jPanel.add(new JButton("AAAAAAAAAAAAA"));

		HighLightTextField txt1 = new HighLightTextField(TxtFieldSE.createStartIcon("fa-glass"),null);
		HighLightTextField txt2 = new HighLightTextField(null,TxtFieldSE.createEndIcon("fa-music"));
		txt2.borderColor = greenColor;
		txt2.highColor = greenColor;
		txt2.iconColor = greenColor;
		HighLightTextField txt3 = new HighLightTextField(TxtFieldSE.createStartIcon("fa-glass"),TxtFieldSE.createEndIcon("fa-music"));
		txt3.borderColor = orangeColor;
		txt3.highColor = orangeColor;
		txt3.iconColor = orangeColor;
		HighLightTextField txt4 = new HighLightTextField(null,TxtFieldSE.createEndInfo("@qq.com"));
		txt4.borderColor = redColor;
		txt4.highColor = redColor;
		txt4.iconColor = redColor;
		
		txt1.setPreferredSize(new Dimension(350,40));
		txt2.setPreferredSize(new Dimension(350,40));
		txt3.setPreferredSize(new Dimension(350,40));
		txt4.setPreferredSize(new Dimension(350,40));
		jPanel.add(txt1);
		jPanel.add(txt2);
		jPanel.add(txt3);
		jPanel.add(txt4);
		
//		jPanel.setLayout(null);
		Container contentPane = frame.getContentPane();
		contentPane.add(jPanel);
		
		
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		frame.setSize(400, 400);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	
	
	public static HighLightTextField createNormalField(){
		return new HighLightTextField();
	}
	
	public static HighLightTextField createGreenField(){
		HighLightTextField txtf = new HighLightTextField();
		txtf.borderColor = greenColor;
		txtf.highColor = greenColor;
		txtf.iconColor = greenColor;
		return txtf;
	}
	
	public static HighLightTextField createOrangeField(){
		HighLightTextField txtf = new HighLightTextField();
		txtf.borderColor = orangeColor;
		txtf.highColor = orangeColor;
		txtf.iconColor = orangeColor;
		return txtf;
	}
	
	public static HighLightTextField createRedField(){
		HighLightTextField txtf = new HighLightTextField();
		txtf.borderColor = redColor;
		txtf.highColor = redColor;
		txtf.iconColor = redColor;
		return txtf;
	}
}

class LightRun implements Runnable {

	private HighLightTextField comp = null;
	private int frames = 3;
	private int interval = 30;
	private boolean lightFlag = true;
	
	public LightRun(HighLightTextField comp){
		this.comp = comp;
	}
	
	public LightRun(HighLightTextField comp,boolean light){
		this.comp = comp;
		this.lightFlag = light;
	}
	
	public void run() {
		try {
			Thread.sleep(interval*2);
			for(int i=0;i<frames;i++){
				if(lightFlag){
					comp.hightLightLevel = i + 1;
				}else{
					comp.hightLightLevel = frames - i;
				}
				comp.repaint();
				Thread.sleep(interval);
			}
			if(!lightFlag){
				comp.haveFocus = false;
				comp.repaint();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}




